<?php

include_once('top.php');

$project = mysql_real_escape_string($_GET['project']);

$total = fetch_only("SELECT SUM(`Hours`) AS `Hours` FROM `ENTRY` WHERE `Project`='$project' GROUP BY `Project`");
$total = $total['Hours'];

$title = fetch_only("SELECT `Title` FROM `PROJECT` WHERE `P_id`='$project'");
$title = $title['Title'];

$milestones = fetch_all("SELECT `MILESTONE`.`Name`, `MILESTONE`.`Due_date`,  `MILESTONE`.`Open`, `MILESTONE`.`Project`, SUM(`ENTRY`.`Hours`) AS `Hours`"
		. "FROM `MILESTONE` LEFT JOIN `ENTRY`"
		. "ON `MILESTONE`.`Name` = `ENTRY`.`Milestone` AND `MILESTONE`.`Project` = `ENTRY`.`Project`"
		. "WHERE `MILESTONE`.`Project` = '$project'"
		. "GROUP BY `MILESTONE`.`Name`"
		. "ORDER BY `Due_date` ASC");

for ($i = 0; $i < count($milestones); $i++) {
	format_date($milestones[$i], 'Due_date', 'm/d/Y');
	
	if ($milestones[$i]['Open'] == '1') {
		$milestones[$i]['Status'] = 'Active';
	} else {
		$milestones[$i]['Status'] = 'Complete';
	}
	
	if (empty($milestones[$i]['Hours'])) {
		$milestones[$i]['Hours'] = '0';
	}
	
	$milestones[$i]['Tasks'] = fetch_all("SELECT `TASK`.`Name`, `TASK`.`Due_date`,  `TASK`.`Open`, `USER`.`First_name`, `USER`.`Last_name`, SUM(`ENTRY`.`Hours`) AS `Hours`"
			. "FROM (`TASK` LEFT JOIN `ENTRY`"
			. "ON `TASK`.`Name` = `ENTRY`.`Task` AND `TASK`.`Milestone` = `ENTRY`.`Milestone` AND `TASK`.`Project` = `ENTRY`.`Project`), `USER`"
			. "WHERE `TASK`.`Employee` = `USER`.`Email` AND `TASK`.`Project` = '{$milestones[$i]['Project']}' AND `TASK`.`Milestone` = '{$milestones[$i]['Name']}'"
			. "GROUP BY `TASK`.`Name`"
			. "ORDER BY `Due_date` ASC");
	
	for ($j = 0; $j < count($milestones[$i]['Tasks']); $j++) {
		format_date($milestones[$i]['Tasks'][$j], 'Due_date', 'm/d/Y');
		
		if ($milestones[$i]['Tasks'][$j]['Open'] == '1') {
			$milestones[$i]['Tasks'][$j]['Status'] = 'Open';
		} else {
			$milestones[$i]['Tasks'][$j]['Status'] = 'Closed';
		}
		
		if (empty($milestones[$i]['Tasks'][$j]['Hours'])) {
			$milestones[$i]['Tasks'][$j]['Hours'] = '0';
		}
		
		$milestones[$i]['Tasks'][$j]['Employee'] = "{$milestones[$i]['Tasks'][$j]['First_name']} {$milestones[$i]['Tasks'][$j]['Last_name']}";
	}
}

?>

<html>
<head>
	<title>GT Project Manager - Project Tasks Report</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<link rel="stylesheet" type="text/css" href="inner.css" />
	<style type="text/css">
		.milestone {
			background-color: #82AFE4;
			font-style: italic;
		}
		
		.milestone td {
			border-top: 3px solid #EBF1DF;
		}
		
		.task td {
			text-align: left;
		}
		
		.task td:first-child {
			padding-left: 12px;
		}
		
		.total {
			text-align: right;
			border-top: 1px solid black;
			font-weight: bold;
			padding-right: 100px;
		}
	</style>
</head>
<body>
	<table cellspacing="0" cellpadding="0" id="main">
		<tr><td id="title">
			Project Tasks Report for <i><?php echo $title ?></i>
			<div class="nav">
				<a href="login.php?action=logout">Log Out</a><br />
				<?php echo $link ?><br />
				<a href="project_info.php?project=<?php echo $project ?>">Project Info</a>
			</div>
		</td></tr>
		<tr>
			<td>
				<table class="subsection summary" cellspacing="0" cellpadding="0">
					<tr>
						<th>Task Name</th>
						<th>Due Date</th>
						<th>Assigned To</th>
						<th>Status</th>
						<th>Total Hours</th>
					</tr>
					<?php
					foreach ($milestones as $milestone) {
					?>
					<tr class="milestone">
						<td><?php echo $milestone['Name'] ?></td>
						<td><?php echo $milestone['Due_date'] ?></td>
						<td>&nbsp;</td>
						<td><?php echo $milestone['Status'] ?></td>
						<td><?php echo $milestone['Hours'] ?></td>
					</tr>
					<?php
					if (is_array($milestone['Tasks']) && count($milestone['Tasks']) > 0) {
					foreach ($milestone['Tasks'] as $task) {
					?>
					<tr class="task">
						<td><a href="task_info.php?name=<?php echo $task['Name'] ?>&milestone=<?php echo $milestone['Name'] ?>&project=<?php echo $milestone['Project'] ?>"><?php echo $task['Name'] ?></a></td>
						<td><?php echo $task['Due_date'] ?></td>
						<td><?php echo $task['Employee'] ?></td>
						<td><?php echo $task['Status'] ?></td>
						<td><?php echo $task['Hours'] ?></td>
					</tr>
					<?php
					}
					} else {
					?>
					<tr class="task">
						<td colspan="5">This milestone has no tasks</td>
					</tr>
					<?php
					}
					}
					?>
					<tr><td colspan="5" class="total">
						Project Total Hours: <?php echo $total ?>
					</td></tr>
				</table>
			</td>
		</tr>
	</table>
</body>
</html>